package com.grq.mis.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.grq.mis.entity.Department;
import com.grq.mis.exception.BusinessException;
import com.grq.mis.exception.ResultEnum;
import com.grq.mis.model.sys.Me;
import com.grq.mis.service.DepartmentService;
import com.grq.mis.validate.DepartmentValidate;
import org.beetl.sql.core.page.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/department")
public class DepartmentController extends BaseController {

    @Autowired
    private DepartmentService departmentService;

    /**
     * 列表
     *
     * @param model
     * @return
     */
    @SaCheckPermission("department:list")
    @GetMapping("/page")
    public String page(
            Model model,
            @RequestParam(defaultValue = "1") long page,
            @RequestParam(defaultValue = "20") int size,
            String key
    ) {

        // 带入参数
        Map<String, Object> paras = new HashMap<>();
        if (!StringUtils.isEmpty(key)) {
            paras.put("key", "%" + key.trim() + "%");
        }

        PageResult<Department> result = departmentService.page("department.page", paras, page, size);
        model.addAttribute("data", result);

        model.addAttribute("key", key);

        return "department/page.html";
    }

    /**
     * 添加商户页面
     *
     * @return
     */
    @SaCheckPermission("department:add")
    @GetMapping("/addForm")
    public String addForm() {

        return "department/add.html";
    }

    @SaCheckPermission("department:add")
    @PostMapping("/add")
    public String add(Department department, Model model, MultipartFile img) {

        // 验证
        DepartmentValidate.add(department);

        Me me = me();
        departmentService.addDepartment(department,me,img);

        return frameSuccess(model, ResultEnum.SUCCESS, "添加商户成功");
    }

    @SaCheckPermission("department:update")
    @GetMapping("/updateForm")
    public String updateForm(@RequestParam Long id, Model model) {

        Department department = departmentService.single(id);
        if (department == null) {
            throw new BusinessException(ResultEnum.DEPARTMENT_IS_NULL);
        }

        model.addAttribute("department", department);

        return "department/update.html";
    }

    @SaCheckPermission("department:update")
    @PostMapping("/update")
    public String update(Department department, Model model, MultipartFile img) {

        // 验证
        DepartmentValidate.update(department);

        departmentService.updateDepartment(department,img);

        return frameSuccess(model, ResultEnum.SUCCESS, "修改商户成功");
    }

    @SaCheckPermission("department:delete")
    @GetMapping("/delete")
    public String delete(@RequestParam Long id, RedirectAttributes attr) {

        departmentService.deleteDepartment(id);

        return pageSuccess(attr, "/department/page", ResultEnum.SUCCESS, "删除商户成功");
    }

}
